package com.cx.controller.czw;

import com.cx.pojo.User;
import com.cx.service.czw.RegisterService;
import com.cx.util.czw.MsgUtil;
import com.cx.util.czw.RandomUtil;
import com.cx.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * 丛志文
 */
@Controller
@RequestMapping("register")
public class RegisterController {

    @Autowired
    RegisterService registerService;



    //用户注册 判断手机号
    @RequestMapping("checkPhone")
    @ResponseBody
    public ResponseResult checkPhone(String phone){
        User user = registerService.userCheckPhone(phone);
        ResponseResult responseResult = new ResponseResult();
        if(null != user){
            responseResult.setMessage("电话号码已经注册过了");
            responseResult.setCode(1);
        }else {
            responseResult.setMessage("电话号码还没注册");
            responseResult.setCode(2);
        }
        return responseResult;
    }

    //获取短信验证码
    @RequestMapping("phoneCode")
    @ResponseBody
    public ResponseResult phoneCode(String phone , String code , HttpSession session){
        System.out.println(phone);
        System.out.println(code);
        System.out.println(session.getAttribute("code"));
        ResponseResult responseResult = new ResponseResult();
        if(session.getAttribute("code").equals(code)){
            String randomPhoneCode = RandomUtil.createRandNum();
            //存在session中
            session.setAttribute("phone",phone);
            session.setAttribute("randomPhoneCode",randomPhoneCode);
            System.out.println(randomPhoneCode);
            int returnCode = MsgUtil.sendMsg(phone,randomPhoneCode);
            //根据返回的值判断获取短信验证码是否成功
            System.out.println(returnCode);
            if(1==returnCode){
                System.out.println("success");
                responseResult.setMessage("获取验证码成功");
                responseResult.setCode(1);
            }else if(2==returnCode) {
                System.out.println("fail");
                responseResult.setMessage("获取验证码失败");
                responseResult.setCode(2);
            }
        }else {
            responseResult.setMessage("验证码错误");
            responseResult.setCode(3);
        }
        return  responseResult;
    }

    //根据电话，验证码 进行最终注册
    @RequestMapping("finalRegister")
    @ResponseBody
    public ResponseResult finalRegister(HttpSession session , String phone , String phoneCode , String pwd, String code) {
        ResponseResult responseResult = new ResponseResult();
        User user = registerService.userCheckPhone(phone);
        //手机号注没注册过的判断
        if(null == user){
        //先进行手机号前后一不一致的判断
           if(session.getAttribute("phone").equals(phone)){
//            if (1 == 1) {
               //再进行验证码的判断
               if(session.getAttribute("code").equals(code)){
//                if (1 == 1) {
                   //再进行短信验证码的判断
                  if (session.getAttribute("randomPhoneCode").equals(phoneCode)) {
//                   if (1 == 1) {
                           //进行注册
                           int affectRows = registerService.userRegister(phone ,pwd);
                           System.out.println("affectRows=" + affectRows);
                           //根据返回影响的行数进行判断
                           if(1 == affectRows){
                               responseResult.setMessage("注册成功");
                               responseResult.setCode(1);
                               session.setAttribute("login","1");
                           }else {
                               responseResult.setMessage("注册失败");
                               responseResult.setCode(2);
                           }
                       }else {
                       //短信验证失败
                       responseResult.setMessage("短信验证码错误");
                       responseResult.setCode(3);
                   }
               }else {
                   //验证码错误
                   responseResult.setMessage("验证码错误");
                   responseResult.setCode(4);
               }
           }else {
               //手机号前后不一样
               responseResult.setMessage("手机号前后不一致");
               responseResult.setCode(5);
           }
        } else {
            responseResult.setMessage("已经注册过了");
            responseResult.setCode(6);
        }

         return responseResult;
    }
}
